package com.recharge.web.controller;


import com.recharge.common.domain.PageDO;
import com.recharge.common.enums.ErrorCode;
import com.recharge.common.result.ResultDO;
import com.recharge.common.result.web.ObjectResponse;
import com.recharge.domain.query.UserQueryDO;
import com.recharge.domain.vo.UserDetailDO;
import com.recharge.domain.vo.UserLoginDO;
import com.recharge.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/user")
@Api(value = "UserContorller | 用户管理")
public class UserContorller extends BaseController {

    @Value("${user.reset-password}")
    private String userPassword;

    @Autowired
    private UserService userService;

    @RequiresPermissions("user:register")
    @RequestMapping(value="/register.html", method = RequestMethod.POST)
    @ApiOperation(value="用户注册", notes="")
    public @ResponseBody
    ObjectResponse register(@RequestBody UserLoginDO domain) {

        ObjectResponse response = new ObjectResponse();

        // 表单验证
        if (!domain.registerVerification()){
            response.setStatus(ErrorCode.PARAM_VERIFICATION_ERROR.getCode());
            response.setMsg(ErrorCode.PARAM_VERIFICATION_ERROR.getMsg());
            return response;
        }

        UserLoginDO userLoginDO = getUserLoginInfo();
        domain.setCreator(userLoginDO.getUsername());

        ResultDO result = userService.add(domain);

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }

//    @RequiresPermissions("user:edit_password")
    @RequestMapping(value="/edit_password.html", method = RequestMethod.POST)
    @ApiOperation(value="修改密码", notes="")
    public @ResponseBody
    ObjectResponse editPassword(@RequestBody UserLoginDO domain) {

        ObjectResponse response = new ObjectResponse();

        ResultDO result = userService.editPassword(domain);

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }


    @RequiresPermissions("user:resetPassword")
//    @RequiresRoles("admin")
    @RequestMapping(value="/resetPassword.html", method = RequestMethod.POST)
    @ApiOperation(value="重置密码", notes="")
    public @ResponseBody
    ObjectResponse resetPassword(@RequestBody UserLoginDO domain) {

        ObjectResponse response = new ObjectResponse();

        domain.setPassword(userPassword);

        domain.setModifier(getUserName());

        ResultDO result = userService.resetPassword(domain);

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }

    @RequiresPermissions("user:edit")
    @RequestMapping(value="/edit.html", method = RequestMethod.POST)
    @ApiOperation(value="修改用户信息", notes="")
    public @ResponseBody
    ObjectResponse edit(@RequestBody UserLoginDO domain) {

        ObjectResponse response = new ObjectResponse();

        UserLoginDO userLoginDO = getUserLoginInfo();
        domain.setModifier(userLoginDO.getUsername());

        ResultDO result = userService.edit(domain);

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }

    @RequiresPermissions("user:lock")
    @RequestMapping(value="/lock.html", method = RequestMethod.POST)
    @ApiOperation(value="锁定用户", notes="")
    public @ResponseBody
    ObjectResponse lock(@RequestBody UserLoginDO domain) {

        ObjectResponse response = new ObjectResponse();

        UserLoginDO userLoginDO = getUserLoginInfo();
        domain.setModifier(getUserName());
        domain.setStatus(0);
        ResultDO result = userService.editStatus(domain);

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }

    @RequiresPermissions("user:unlock")
    @RequestMapping(value="/unlock.html", method = RequestMethod.POST)
    @ApiOperation(value="解除用户锁定", notes="")
    public @ResponseBody
    ObjectResponse unlock(@RequestBody UserLoginDO domain) {

        ObjectResponse response = new ObjectResponse();

        UserLoginDO userLoginDO = getUserLoginInfo();
        domain.setModifier(userLoginDO.getUsername());
        domain.setStatus(1);
        ResultDO result = userService.editStatus(domain);

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }

    @RequiresPermissions("user:get")
    @RequestMapping(value="/get.html", method = RequestMethod.POST)
    @ApiOperation(value="获取用户信息", notes="")
    public @ResponseBody
    ObjectResponse<UserLoginDO> get(@RequestBody UserLoginDO domain) {

        ObjectResponse<UserLoginDO> response = new ObjectResponse<UserLoginDO>();

        ResultDO result = userService.get(domain.getId());

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        } else {
            try {
                response.setData((UserLoginDO) result.getModel(ResultDO.FIRST_MODEL_KEY));
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return response;
    }

    @RequiresPermissions("user:delete")
    @RequestMapping(value="/delete.html", method = RequestMethod.POST)
    @ApiOperation(value="删除用户信息", notes="")
    public @ResponseBody
    ObjectResponse delete(@RequestBody UserLoginDO domain) {

        ObjectResponse response = new ObjectResponse();
        UserLoginDO userLoginDO = getUserLoginInfo();

        ResultDO result = userService.remove(domain.getId(), userLoginDO.getUsername());

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }

    @RequiresPermissions("user:page")
    @RequestMapping(value="/page.html", method = RequestMethod.POST)
    @ApiOperation(value="分页查询用户", notes="")
    public @ResponseBody  ObjectResponse<PageDO> page(@RequestBody UserQueryDO query) {

        ObjectResponse<PageDO> response = new ObjectResponse<>();
        System.out.println(query.getCurrentPage());
        ResultDO result = userService.find(query);

        if(result.isSuccess()) {
            response.setData((PageDO) result.getModel(ResultDO.FIRST_MODEL_KEY));
        } else {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }

        return response;
    }

}
